package cn.kent.base.interrupt;

/**
 * interrupt() 方法是设置了中断标志
 * 如果线程正处于sleep、wait、join而被阻塞状态时，interrupt会导致抛出Interrupted异常
 */
public class InterruptTest {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread() + " 继续运行中...");
                }
            }
        });
        thread.start();
        // 主线程休眠1s，以便让子线程输出
        Thread.sleep(1000);
        System.out.println("main thread interrupt child thread.");
        thread.interrupt(); // 设置了中断标志
        // 等待子线程执行结束
        thread.join();
        System.out.println("main is over");
    }
}
